package com.zy.ems.common.util;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zy.ems.common.http.HttpClientUtil;

/**
 * 有道翻译工具类。
 * 
 * @author Luffy
 *
 */
public class TransUtil {
    private static Logger logger = LoggerFactory.getLogger(TransUtil.class);

    public static String translation(String translate) {
        String obj = "";
        try {
            translate = new String((translate).getBytes("UTF-8"), "UTF-8");
            String url = "http://fanyi.youdao.com/openapi.do";
            Map<String, String> paramaters = new HashMap<String, String>();
            paramaters.put("keyfrom", "zpwyxgs");
            paramaters.put("key", "73613364");
            paramaters.put("type", "data");
            paramaters.put("doctype", "json");
            paramaters.put("version", "1.1");
            paramaters.put("q", translate);
            String executeGet = HttpClientUtil.executeGet(url, paramaters);
            Map<String, Object> translation = JsonUtils
                    .readJson2Entity(executeGet, Map.class);
            Object object = translation.get("translation");
            obj = object.toString();
            if (obj.indexOf("[") != -1) {
                obj = obj.substring(1, obj.length() - 1);
            }
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return obj;
    }

}
